package android.slc.popup;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.os.Build;
import android.slc.popup.fast.ShadowPopupWindow;
import android.slc.popup.impl.AlertDialogOperateImpl;
import android.slc.popup.impl.LoadingDialogOperateImpl;
import android.slc.popup.impl.PopupOperateImpl;
import android.slc.popup.impl.ShadowPopupOperateImpl;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.appcompat.widget.ListPopupWindow;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/04/07 下午 5:11
 */
public class SlcPopup2 {

    private static final Map<String, BaseOperate<?>> operateMap = new LinkedHashMap<>();

    public static void addOperate(String key, BaseOperate<?> baseOperate) {
        if (!TextUtils.isEmpty(key) && baseOperate != null) {
            operateMap.put(key, baseOperate);
        }
    }

    public static void removeOperate(String key) {
        if (!TextUtils.isEmpty(key)) {
            operateMap.remove(key);
        }
    }

    public static void dismissByKey(String key) {
        BaseOperate<?> baseOperate = getOperateByKey(key);
        if (baseOperate != null) {
            baseOperate.dismiss();
        }
    }

    public static BaseOperate<?> getOperateByKey(String key) {
        return operateMap.get(key);
    }

    public static boolean existOperateByKey(String key) {
        return getOperateByKey(key) != null;
    }

    /**
     * Return the width of screen, in pixel.
     *
     * @return the width of screen, in pixel
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }

    /**
     * Return the height of screen, in pixel.
     *
     * @return the height of screen, in pixel
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }

    public interface BaseOperate<T> {

        BaseOperate<T> setKey(String key);

        BaseOperate<T> setCancelable(boolean cancelable);

        boolean isCancelable();

        String getKey();

        void dismiss();

        void show(T targetPopup);
    }

    public interface DialogOperate extends BaseOperate<Dialog> {

        DialogOperate setOnCancelListener(DialogInterface.OnCancelListener onCancelListener);

        DialogOperate setOnDismissListener(DialogInterface.OnDismissListener onDismissListener);

        DialogOperate setOnShowListener(DialogInterface.OnShowListener onShowListener);

        Dialog getDialog();

    }

    public interface LoadingDialogOperate extends DialogOperate {
        void updateMessage(String message);
    }

    public interface AlertDialogOperate extends DialogOperate {

    }

    public interface PopupOperate extends BaseOperate<ListPopupWindow> {

        PopupOperate setOnDismissListener(PopupWindow.OnDismissListener onDismissListener);

        ListPopupWindow getListPopupWindow();

    }

    public interface ShadowPopupOperate extends BaseOperate<ShadowPopupWindow> {

        ShadowPopupOperate setOnDismissListener(ShadowPopupWindow.OnDismissListener onDismissListener);

        ShadowPopupOperate setOnShowListener(ShadowPopupWindow.OnShowListener onShowListener);

        ShadowPopupWindow getListPopupWindow();

    }

    public static LoadingDialogOperate createLoadingDialogOperate() {
        return new LoadingDialogOperateImpl();
    }

    public static AlertDialogOperate createAlertDialogOperate() {
        return new AlertDialogOperateImpl();
    }

    public static PopupOperate createPopupOperate() {
        return new PopupOperateImpl();
    }

    public static ShadowPopupOperate createShadowPopupOperate() {
        return new ShadowPopupOperateImpl();
    }
}
